{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分词结果: ['原告', '在', '昨天', '正式', '向', '法院', '提起', '诉讼', '，', '将', '于', '后天', '开庭']\n",
      "词性标注结果: ['n', ' ', 'p', ' ', 'n', 't', ' ', 'a', ' ', 'p', ' ', 'n', ' ', 'v', ' ', 'v', ' ', 'w', 'p', ' ', 'd', ' ', 'p', ' ', 'n', 't', ' ', 'v']\n",
      "依存句法分析结果: 7:SBV 7:ADV 7:ADV 7:ADV 7:ADV 5:POB 0:HED 7:VOB 7:WP 13:ADV 13:ADV 11:POB 7:COO\n",
      "语义角色标注结果: [(6, [('A0', (0, 0)), ('TMP', (1, 1)), ('ADV', (3, 3)), ('A2', (4, 5)), ('A1', (7, 7))]), (12, [('A0', (0, 0)), ('ADV', (9, 9)), ('TMP', (10, 11))])]\n",
      "句子中谓词的数量： 2\n",
      "句子中的谓词: ['提起', '开庭']\n",
      "句子中谓词的索引: 6\t12\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from pyltp import Segmentor\n",
    "from pyltp import Postagger\n",
    "from pyltp import Parser\n",
    "from pyltp import SementicRoleLabeller\n",
    "LTP_DIR='ltp_data_v3.4.0'\n",
    "cws_model_path=os.path.join(LTP_DIR,'cws.model')\n",
    "segmentor=Segmentor(cws_model_path)\n",
    "words=segmentor.segment('原告在昨天正式向法院提起诉讼，将于后天开庭')\n",
    "print('分词结果:',list(words))\n",
    "pos_model_path=os.path.join(LTP_DIR,'pos.model')\n",
    "postagger=Postagger(pos_model_path)\n",
    "postags=postagger.postag(words)\n",
    "print('词性标注结果:',list(' '.join(postags)))\n",
    "par_model_path=os.path.join(LTP_DIR,'parser.model')\n",
    "parser=Parser(par_model_path)\n",
    "arcs=parser.parse(words,postags)\n",
    "print('依存句法分析结果:',\" \".join('%d:%s' % (arc[0],arc[1])for arc in arcs))\n",
    "lab_model_path=os.path.join(LTP_DIR,'pisrl_win.model')\n",
    "labeller=SementicRoleLabeller(lab_model_path)\n",
    "roles=labeller.label(words,postags,arcs)\n",
    "print('语义角色标注结果:',roles)\n",
    "print('句子中谓词的数量：',len(roles))\n",
    "verbs=[words[role[0]] for role in roles]\n",
    "print('句子中的谓词:',verbs)\n",
    "print('句子中谓词的索引:','\\t'.join('%d' % role[0] for role in roles))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
